<template>
  <el-row class="container">
    <el-col :span="15">
      <el-row>
        <el-col :span="5">
          <h2>型号设置</h2>
        </el-col>
        <el-col :span="10">
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item label="">
              <el-input v-model="form.name" placeholder="型号名称" clearable size="mini"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="mini">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-table
        border
        :data="listData.items">
        <el-table-column
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="editItem(scope.row.id)" >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="deleteItem(scope.row.id)" >删除
            </el-button>
            <add-model-dialog :show.sync="editDialog" @success="initData()" v-if="scope.row.id===editId" :isedit="true" :dataItem="scope.row"></add-model-dialog>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          align="center">
        </el-table-column>
        <el-table-column
          prop="name"
          label="型号名称"
          align="center">
        </el-table-column>
        <el-table-column
          prop="supplier"
          label="厂商"
          align="center">
        </el-table-column>
        <el-table-column
          prop="content"
          label="说明"
          align="center">
        </el-table-column>
        <el-table-column
          prop="createAt"
          label="创建日期"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.createAt | format}}</div>
          </template>
        </el-table-column>
      </el-table>
      <app-pagination
        :currentPage.sync="listData.page"
        :size.sync="listData.size"
        :total="listData.total"
        @handleCurrentChange="initData()"
        @handleSizeChange="initData()"
      ></app-pagination>
      <div class="add_btn_box">
        <el-button @click="addDialog = true" type="primary" size="mini">新增</el-button>
      </div>
    </el-col>
    <add-model-dialog :show.sync="addDialog" @success="initData()"></add-model-dialog>
  </el-row>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import AppPagination from '@/components/AppPagination'
  import AddModelDialog from './AddModelDialog'

  export default {
    components: {
      'app-pagination': AppPagination,
      'add-model-dialog': AddModelDialog,
    },
    data() {
      return {
        listData: {
          page: 1,
          size: 10,
          items: null,
          total: null,
          totalPage: null,
          name: '',
        },
        form: {
          name: '',
        },
        addDialog: false,
        editDialog: false,
        //编辑条目id
        editId:null,
      }
    },
    methods: {
      initData() {
        ApiClient.get('/backend/model/list.do', {
          page: this.listData.page,
          size: this.listData.size,
          name: this.form.name
        }).then(data => {
          console.log(data)
          this.listData = data
        })
      },
      deleteItem(id) {
        ApiClient.put(`/backend/model/${id}/delete.do`).then(data => {
          this.initData()
        }).catch()
      },
      editItem(id){
        this.editId = id;
        this.editDialog = true;
      },
      onSubmit() {
        this.listData.page = 1;
        this.listData.size = 10;
        this.initData()
      },
    },
    mounted() {
      this.initData()
    }
  }
</script>
<style scoped lang="scss">
  .container {
    .add_btn_box {
      margin-top: 20px;
      .el-button {
      }
    }
  }
</style>
